package com.atguigu.gmall.web.controller;


import com.atguigu.gmall.feign.item.SkuDetailFeignClient;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Map;

/**
 * @author 钟涛
 * @version 1.0
 * @description: TODO
 * @date 2021/12/10 11:30
 */
@Controller
public class SkuInfoController {
    @Autowired
    private SkuDetailFeignClient skuDetailFeignClient;
    @GetMapping("/{id}.html")
    public String skuDetail(@PathVariable("id") Long skuId, Model model) throws JsonProcessingException {
        //1.查出当前商品的所有信息
        /**
         * 需要远程调用的信息:
         * 1.categoryViews：三级分类信息
         * 2.skuInfo:sku的基本信息
         * 3.price  价格
         * 4.spuSaleAttrList  spu销售属性列表
         * 5.ValueSkuJson   保存一对销售属性组合对应是那个shangp
         */

        Map<String, Object> map = skuDetailFeignClient.skuDetails(skuId);
        ObjectMapper mapper = new ObjectMapper();

        Object valuesSkuJson = map.get("valuesSkuJson");
        map.put("valuesSkuJson",mapper.writeValueAsString(valuesSkuJson));
        //把map中所有数据，按照key-value的形式放入请求域中
        model.addAllAttributes(map);
        return "item/index";
    }
}
